package com.dycx.user.converter;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dycx.user.dto.UserDTO;
import com.dycx.user.entity.UserDO;
import com.dycx.user.enums.AgencyGradeEnum;
import com.dycx.user.enums.VipGradeEnum;
import com.dycx.user.vo.UserVO;
import lombok.Data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 会员(User)实体转换类
 *
 * @author wuzongbin
 * @since 2023-12-25 09:59:21
 */


@Data
public class UserConverter implements Serializable {
    public static UserDO dtoToEntity(UserDTO dto) {
        if (dto == null) return null;
        UserDO entity = BeanUtil.toBean(dto, UserDO.class);
        return entity;
    }

    public static UserVO entityToVo(UserDO entity) {
        if (entity == null) return null;
        UserVO vo = BeanUtil.toBean(entity, UserVO.class);
        vo.setAgencyGradeName(AgencyGradeEnum.converter(vo.getAgencyGrade()).getName());
        vo.setAgencyGradeRate(AgencyGradeEnum.converter(vo.getAgencyGrade()).getRate());
        vo.setVipGradeName(VipGradeEnum.converter(vo.getVipGrade()).getName());
        return vo;
    }

    public static List<UserVO> entityToVoList(List<UserDO> entityList) {
        List<UserVO> voList = new ArrayList<>();
        for (UserDO entity : entityList) {
            voList.add(entityToVo(entity));
        }
        return voList;
    }

    public static Page<UserVO> entityToVoPage(Page<UserDO> page) {

        List<UserVO> newRecords = new ArrayList<>();

        List<UserDO> records = page.getRecords();
        for (UserDO entity : records) {
            newRecords.add(entityToVo(entity));
        }

        Page<UserVO> voPage = new Page<>();
        voPage.setRecords(newRecords);
        voPage.setPages(page.getPages());
        voPage.setTotal(page.getTotal());
        voPage.setSize(page.getSize());
        voPage.setCurrent(page.getCurrent());

        return voPage;
    }

}
